﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Components;

namespace WorkflowDesigner
{
    public class Port : Canvas
    {
        #region props
        public PortType PortType { get; set; }
        public Size PortSize { get; set; }
        public Node OwnerNode { get; set; }
        public Point Position
        {
            get
            {
                double x = Convert.ToDouble(this.GetValue(Canvas.LeftProperty));
                double y = Convert.ToDouble(this.GetValue(Canvas.TopProperty));
                return new Point(x,y);
            }
        }
        #endregion

        public Port(PortType portType, Node owner)
        {
            this.PortType = portType;
            this.OwnerNode = owner;
            this.PortSize=new Size(6,6);
            this.Loaded += new RoutedEventHandler(Port_Loaded);
            this.MouseEnter += new MouseEventHandler(Port_MouseEnter);
            this.MouseLeave += new MouseEventHandler(Port_MouseLeave);
        }

        void Port_MouseLeave(object sender, MouseEventArgs e)
        {
            this.RenderTransform = null;
        }

        void Port_MouseEnter(object sender, MouseEventArgs e)
        {
            ScaleTransform scaleTransform=new ScaleTransform();
            scaleTransform.ScaleX = 1.5;
            scaleTransform.ScaleY = 1.5;
            TransformGroup transformGroup = new TransformGroup();
            transformGroup.Children.Add(scaleTransform);
            this.RenderTransform = transformGroup;
        }

        void Port_Loaded(object sender, RoutedEventArgs e)
        {
            //double x = Convert.ToDouble(this.OwnerNode.GetValue(Canvas.LeftProperty));
            //double y = Convert.ToDouble(this.OwnerNode.GetValue(Canvas.TopProperty));
            // this.UpdatePosition(new Point(x,y), this.OwnerNode.ImageSize);
            this.Draw();
        }

        public void Draw()
        {
            Ellipse ellipse=new Ellipse();
            ellipse.SetValue(WidthProperty,this.PortSize.Width);
            ellipse.SetValue(HeightProperty,this.PortSize.Height);
            ellipse.StrokeThickness = 1;
            switch(this.PortType)
            {
                case PortType.Input:
                    ellipse.Stroke = new SolidColorBrush(Colors.DarkGray);
                    ellipse.Fill = new SolidColorBrush(Colors.Blue);
                    break;
                case PortType.SuccessOutput:
                    ellipse.Stroke = new SolidColorBrush(Colors.DarkGray);
                    ellipse.Fill = new SolidColorBrush(Colors.Green);
                    break;
                case PortType.ErrorOutput:
                    ellipse.Stroke = new SolidColorBrush(Colors.DarkGray);
                    ellipse.Fill = new SolidColorBrush(Colors.Red);
                    break;
                default:
                    break;
            }
            this.Children.Add(ellipse);
        }

        public bool HitTest(Point mousePos)
        {
            double distance = Math.Pow(mousePos.X - this.Position.X, 2) + Math.Pow(mousePos.Y - this.Position.Y, 2);
            if(distance < 64)
                return true;
            else
                return false;
        }
    }
}
